const Service = require('egg').Service;
// 引入md5加密
var md5 = require('md5-node');

class UserService extends Service {
  // 从数据库获取账号，且返回给控制器
  async getUser() {
    try {
      let userList = await this.app.model.User.findAll({
        attributes: [ 'id', 'user_name', 'user_phone', 'user_role', 'user_status' ],
        include: {
          model: this.app.model.Role
        }
      });
      // 修改状态为布尔
      userList.forEach( item => {
        if (item.user_status === 0) {
          item.user_status = false;
        }
        if (item.user_status === 1) {
          item.user_status = true;
        }
      });
      return userList;
    } catch (error) {
      return false;
    }
  }
  // 新增
  async addUser(userInfo) {
    try {
      let { username, password, phone } = userInfo;
      await this.app.model.User.create({
        user_name: username,
        user_password: md5(password),
        user_phone: phone,
        // 默认角色和状态
        user_role: 1,
        user_status: 1,
      });
      return true;
    } catch (error) {
      return false;
    }
  }
  //  删除
  async deleteUser(id) {
    try {
      await this.app.model.User.destroy(
        { where: { id: id } }
      );
      return true;
    } catch (error) {
      return false;
    }
  }
  // 修改账号
  async changeUser(id, newInfo) {
    try {
      let { newPhone, newRole, newStatus } = newInfo;
      console.log(newPhone, newRole, newStatus);
      if (newStatus === true) {
        newStatus = 1;
      } else {
        newStatus = 0;
      }
      await this.app.model.User.update({
        user_phone: newPhone,
        user_role: newRole,
        user_status: newStatus
      }, {
        where: {
          id
        }
      });
      return true;
    } catch (error) {
      return false;
    }
  }
  // 根据id获取一个用户信息
  async getOneUser(id) {
    try {
      return await this.app.model.User.findOne({
        attributes: [ 'id', 'user_name', 'user_role' ],
        include: {
          model: this.app.model.Role
        },
        where: {
          id: id
        }
      });
    } catch (error) {
      return false;
    }
  }
  // 登录验证
  async loginCheck(userData) {
    try {
      let { username, password } = userData;
      // 根据用户名去判断密码
      let mpwd = md5(password);
      // 查找是否有这个用户
      let rel = await this.app.model.User.findOne({
        where: {
          user_name: username
        }
      });
      // 判断用户输入的帐号密码是否和数据库的一样
      if (rel) {
        let pass = rel.dataValues.user_password;
        let usr = rel.dataValues.user_name;
        let status = rel.dataValues.user_status;
        let role = rel.dataValues.user_role;
        // 如果用户和密码正确，并且状态开启
        if (pass === mpwd && usr === username && status === 1) {
          let user_jwt = { admin: username };
          const token = this.app.jwt.sign(user_jwt, this.app.config.jwt.secret);
          return { state: true, result: token, role };
        } else {
          return { state: false, result: '帐号或密码错误或账号被停用' };
        }
      } else {
        return { state: null, result: '没有这个帐号' };
      }
    } catch (e) {
      return '服务器错误';
    }
  }
}

module.exports = UserService;
